import { ALL_CATEGORY_ITEM, NEW_CATEGORY_LIST } from '@/constants'
import { getCategory } from '@/api/category'

export default {
  namespaced: true,
  state: {
    categoryList: NEW_CATEGORY_LIST
  },
  mutations: {
    setCategoryList(state, payload) {
      state.categoryList = [ALL_CATEGORY_ITEM, ...payload]
    }
  },
  actions: {
    async getCategory({ commit }) {
      const {
        code,
        data: { categorys }
      } = await getCategory()
      if (code !== 200) return commit('setCategoryList', [])
      commit('setCategoryList', categorys)
    }
  }
}
